package com.winit.core.server;

import com.winit.core.server.base.CmdOutputHandler;

/**
 * Created by wangjian on 16/8/11.
 */
public class SpringBootServer extends Server {

    //工程所在目录
    private String serverPath;
    //发布包名称
    private String warName;


    public SpringBootServer(String ip, String userName, Integer port, String serverPath, String warName) throws Exception {
        super(ip, userName, port);
        this.serverPath = serverPath;
        this.warName = warName;
    }

    /**
     * 停止服务
     */
    public void doStopServer(CmdOutputHandler handler) throws Exception {
        this.doExec(handler, this.getStopServerCmd());
    }

    public void doStopServer() throws Exception {
        this.doExec(this.getStopServerCmd());
    }

    private String getStopServerCmd() {
        StringBuffer cmd = new StringBuffer();
        cmd.append("ps -ef | grep -w '").append(this.serverPath).append("/").append(this.warName)
                .append(".jar' | grep -v 'grep' |grep -v 'tail' | grep -v 'ssh' |awk '{print $2}'| xargs kill -9");
        return cmd.toString();
    }

    /**
     * 启动服务
     */
    public void doStartServer(CmdOutputHandler handler) throws Exception {
        this.doExec(handler, getStartCmd());
    }

    public void doStartServer() throws Exception {
        this.doExec(getStartCmd());
    }

    private String getStartCmd(){
        StringBuffer str = new StringBuffer();
        str.append("source /etc/profile;export JAVA_HOME=/usr/java/jdk1.7.0_79;export PATH=$PATH:$JAVA_HOME/bin;nohup ")
                .append(this.serverPath).append("/").append(this.warName).append(".jar </dev/null >/dev/null 2>&1 &");
        return str.toString();
    }

}
